package ensemble

import javafx.scene.Node
import javafx.scene.image.Image
import javafx.scene.layout.{Pane, VBox, Priority}
import javafx.geometry.{Insets, HPos, VPos}
import net.oschina.oldpig.snout.FxBuilder
import ensemble.controls.SimplePropertySheet

trait EnsembleSample extends Pane with DelayedInit {

  import FxBuilder.builderParent

  val controls = Seq.empty[SimplePropertySheet.PropDesc]

  protected def init(): Unit
  protected def onInit(): Unit = {}

  def play(): Unit = {}

  def stop(): Unit = {}

  def content: Any

  override def delayedInit(body: => Unit) {
    body
    builderParent.withValue(this) {
      content
    }
    init()
    onInit()
  }
}

abstract class DynamicSizeSample extends EnsembleSample {
  final override def init() = {
    VBox.setVgrow(this, Priority.ALWAYS)
    setMaxWidth(Double.MaxValue)
    setMaxHeight(Double.MaxValue)
  }

  override def layoutChildren() {
    import collection.JavaConverters._
    val children = getManagedChildren.asInstanceOf[java.util.List[Node]].asScala
    val (width, height, top, right, left, bottom) = (getWidth, getHeight, getInsets.getTop, getInsets.getRight, getInsets.getLeft, getInsets.getBottom)
    for (child <- children) {
      layoutInArea(child, left, top, width - left - right, height - top - bottom,
        0, Insets.EMPTY, true, true, HPos.CENTER, VPos.CENTER)
    }
  }
}

abstract class FixSizeSample(width: Double, height: Double) extends EnsembleSample {
  final override def init(): Unit = {
    setMaxSize(width, height)
    setPrefSize(width, height)
    setMaxSize(width, height)
  }

  override def layoutChildren() {
    super.layoutChildren()
  }
}


object EnsembleSample {
  val ICON_48 = new Image(classOf[Ensemble2].getResourceAsStream("images/icon-48x48.png"))
  val BRIDGE = new Image(classOf[Ensemble2].getResourceAsStream("images/sanfran.jpg"))
}

